Qt5.8制作的简单数字计算器
#include "mainwindow.h"#include "ui_mainwindow.h"
MainWindow::MainWindow(QWidget *parent) : QMainWindow(parent), ui(new Ui::MainWindow){ ui->setupUi(this);
/***********开机显示*************/// widget = new QWidget(this);// widget->setFixedSize(222,418);// timerOpen = new QTimer;// labelOpen = new QLabel(widget);// QImage *img = new QImage; //新建一个image对象// img->load(":/Photo/开机界面.png"); //将图像资源载入对象img,注意路径,可点进图片右键复制路径// this->labelOpen->setPixmap(QPixmap::fromImage(*img)); //将图片放入label,使用setPixmap,注意指针*img// timerOpen->start(3000);// connect(timerOpen,SIGNAL(timeout()),this,SLOT(OpenPhoto_Slot())); /******************************/
this->setWindowTitle("计算器"); this->setFixedSize(222,418); this->setWindowIcon(QIcon(":/Photo/ICON.png")); QPalette color; color.setColor(QPalette::Background,Qt::darkCyan);//设置背景颜色 this->setPalette(color);
this->setPushButtonSize(); this->addControlUi(); this->setMenuIcon(); ui->lcdNumber->setDigitCount(10);}
//void MainWindow::OpenPhoto_Slot()//{// qDebug()<<"this OPenPhoto_Slot";// delete widget;// timerOpen->stop();//}
void MainWindow::setMenuIcon(){ ui->actioncheng->setIcon(QIcon(":/Photo/标准.png")); ui->actionscience->setIcon(QIcon(":/Photo/科学.png")); ui->actionprogrammer->setIcon(QIcon(":/Photo/程序员.png"));}
void MainWindow::setPushButtonSize(){ ui->PER_CENT->setFixedSize(51,51);// % ui->ZERO->setFixedSize(51,51); // 0 ui->POINT->setFixedSize(51,51); // . ui->ONE->setFixedSize(51,51); // 1 ui->TWO->setFixedSize(51,51); // 2 ui->THREE->setFixedSize(51,51); // 3 ui->FOUR->setFixedSize(51,51); // 4 ui->FIVE->setFixedSize(51,51); // 5 ui->SIX->setFixedSize(51,51); // 6 ui->SEVEN->setFixedSize(51,51); // 7 ui->EIGHT->setFixedSize(51,51); // 8 ui->NINE->setFixedSize(51,51); // 9 ui->CLEAR->setFixedSize(51,51); // C ui->DIVIDE->setFixedSize(51,51); //"/" ui->RIDE->setFixedSize(51,51); // X ui->SUB->setFixedSize(51,51); // - ui->ADD->setFixedSize(51,51); // ui->EQUAL->setFixedSize(51,101); // = ui->BACK->setFixedSize(51,51); ui->lcdNumber->setFixedSize(201,61);}
void MainWindow::addControlUi(){ ui->ZERO-> setIcon(QIcon(":/Photo/0.png")); ui->ONE-> setIcon(QIcon(":/Photo/1.png")); ui->TWO-> setIcon(QIcon(":/Photo/2.png")); ui->THREE-> setIcon(QIcon(":/Photo/3.png")); ui->FOUR-> setIcon(QIcon(":/Photo/4.png")); ui->FIVE-> setIcon(QIcon(":/Photo/5.png")); ui->SIX-> setIcon(QIcon(":/Photo/6.png")); ui->SEVEN-> setIcon(QIcon(":/Photo/7.png")); ui->EIGHT-> setIcon(QIcon(":/Photo/8.png")); ui->NINE-> setIcon(QIcon(":/Photo/9.png")); ui->BACK-> setIcon(QIcon(":/Photo/back.png")); ui->ADD-> setIcon(QIcon(":/Photo/ .png")); ui->SUB-> setIcon(QIcon(":/Photo/-.png")); ui->CLEAR-> setIcon(QIcon(":/Photo/C.png")); ui->DIVIDE->setIcon(QIcon(":/Photo/X.png")); ui->RIDE-> setIcon(QIcon(":/Photo/除.png")); ui->PER_CENT->setIcon(QIcon(":/Photo/%.png")); ui->POINT-> setIcon(QIcon(":/Photo/..png")); ui->EQUAL-> setIcon(QIcon(":/Photo/=.png"));
ui->ZERO-> setIconSize(QSize(51,51)); ui->ONE-> setIconSize(QSize(51,51)); ui->TWO-> setIconSize(QSize(51,51)); ui->THREE-> setIconSize(QSize(51,51)); ui->FOUR-> setIconSize(QSize(51,51)); ui->FIVE-> setIconSize(QSize(51,51)); ui->SIX-> setIconSize(QSize(51,51)); ui->SEVEN-> setIconSize(QSize(51,51)); ui->EIGHT-> setIconSize(QSize(51,51)); ui->NINE-> setIconSize(QSize(51,51)); ui->BACK-> setIconSize(QSize(51,51)); ui->ADD-> setIconSize(QSize(51,51)); ui->SUB-> setIconSize(QSize(51,51)); ui->CLEAR-> setIconSize(QSize(51,51)); ui->DIVIDE->setIconSize(QSize(51,51)); ui->RIDE-> setIconSize(QSize(51,51)); ui->PER_CENT->setIconSize(QSize(51,51)); ui->POINT-> setIconSize(QSize(51,51)); ui->EQUAL-> setIconSize(QSize(51,101));}
MainWindow::~MainWindow(){ delete ui;}
/**********- 0 -***********/void MainWindow::on_ZERO_pressed(){ ui->ZERO-> setIcon(QIcon(":/Photo/0白.png")); ui->ZERO-> setIconSize(QSize(51,51)); if(line.isEmpty()) { line = "0"; ui->lcdNumber->display("0"); } else { line = "0"; ui->lcdNumber->display(line); }}
void MainWindow::on_ZERO_released(){ ui->ZERO-> setIcon(QIcon(":/Photo/0.png")); ui->ZERO-> setIconSize(QSize(51,51));}/**********- 1 -***********/void MainWindow::on_ONE_pressed(){ ui->ONE-> setIcon(QIcon(":/Photo/1白.png")); ui->ONE-> setIconSize(QSize(51,51)); line = "1"; ui->lcdNumber->display(line);}
void MainWindow::on_ONE_released(){ ui->ONE-> setIcon(QIcon(":/Photo/1.png")); ui->ONE-> setIconSize(QSize(51,51));}/**********- 2 -***********/void MainWindow::on_TWO_pressed(){ ui->TWO-> setIcon(QIcon(":/Photo/2白.png")); ui->TWO-> setIconSize(QSize(51,51)); line = "2"; ui->lcdNumber->display(line);}
void MainWindow::on_TWO_released(){ ui->TWO-> setIcon(QIcon(":/Photo/2.png")); ui->TWO-> setIconSize(QSize(51,51));}/**********- 3 -***********/void MainWindow::on_THREE_pressed(){ ui->THREE-> setIcon(QIcon(":/Photo/3白.png")); ui->THREE-> setIconSize(QSize(51,51)); line = "3"; ui->lcdNumber->display(line);}
void MainWindow::on_THREE_released(){ ui->THREE-> setIcon(QIcon(":/Photo/3.png")); ui->THREE-> setIconSize(QSize(51,51));}/**********- 4 -***********/void MainWindow::on_FOUR_pressed(){ ui->FOUR-> setIcon(QIcon(":/Photo/4白.png")); ui->FOUR-> setIconSize(QSize(51,51)); line = "4"; ui->lcdNumber->display(line);}
void MainWindow::on_FOUR_released(){ ui->FOUR-> setIcon(QIcon(":/Photo/4.png")); ui->FOUR-> setIconSize(QSize(51,51));}/**********- 5 -***********/void MainWindow::on_FIVE_pressed(){ ui->FIVE-> setIcon(QIcon(":/Photo/5白.png")); ui->FIVE-> setIconSize(QSize(51,51)); line = "5"; ui->lcdNumber->display(line);}
void MainWindow::on_FIVE_released(){ ui->FIVE-> setIcon(QIcon(":/Photo/5.png")); ui->FIVE-> setIconSize(QSize(51,51));}/**********- 6 -***********/void MainWindow::on_SIX_pressed(){ ui->SIX-> setIcon(QIcon(":/Photo/6白.png")); ui->SIX-> setIconSize(QSize(51,51)); line = "6"; ui->lcdNumber->display(line);}
void MainWindow::on_SIX_released(){ ui->SIX-> setIcon(QIcon(":/Photo/6.png")); ui->SIX-> setIconSize(QSize(51,51));}/**********- 7 -***********/void MainWindow::on_SEVEN_pressed(){ ui->SEVEN-> setIcon(QIcon(":/Photo/7白.png")); ui->SEVEN-> setIconSize(QSize(51,51)); line = "7"; ui->lcdNumber->display(line);}
void MainWindow::on_SEVEN_released(){ ui->SEVEN-> setIcon(QIcon(":/Photo/7.png")); ui->SEVEN-> setIconSize(QSize(51,51));}/**********- 8 -***********/void MainWindow::on_EIGHT_pressed(){ ui->EIGHT-> setIcon(QIcon(":/Photo/8白.png")); ui->EIGHT-> setIconSize(QSize(51,51)); line = "8"; ui->lcdNumber->display(line);}
void MainWindow::on_EIGHT_released(){ ui->EIGHT-> setIcon(QIcon(":/Photo/8.png")); ui->EIGHT-> setIconSize(QSize(51,51));}/**********- 9 -***********/void MainWindow::on_NINE_pressed(){ ui->NINE-> setIcon(QIcon(":/Photo/9白.png")); ui->NINE-> setIconSize(QSize(51,51)); line = "9"; ui->lcdNumber->display(line);}
void MainWindow::on_NINE_released(){ ui->NINE-> setIcon(QIcon(":/Photo/9.png")); ui->NINE-> setIconSize(QSize(51,51));}/**********- back -***********/void MainWindow::on_BACK_pressed(){ ui->BACK-> setIcon(QIcon(":/Photo/back白.png")); ui->BACK-> setIconSize(QSize(51,51)); qDebug()<<line; line.chop(1); //倒位删除,即是从字符串后面开始按位删除 if(line.isEmpty()) { ui->lcdNumber->display(0); } else { ui->lcdNumber->display(line); }}
void MainWindow::on_BACK_released(){ ui->BACK-> setIcon(QIcon(":/Photo/back.png")); ui->BACK-> setIconSize(QSize(51,51));}/**********- -***********/void MainWindow::on_ADD_pressed(){ ui->ADD-> setIcon(QIcon(":/Photo/ 白.png")); ui->ADD-> setIconSize(QSize(51,51)); pointFlag = true; flag = 1; temp1 = line; //获取前面输入的字符,并临时保存起来 qDebug()<<temp1; line.clear(); //清空字符数组 sum = temp1.toDouble(); //将字符转换成浮点型 qDebug()<<sum; ui->lcdNumber->display(0);//显示清空状态}
void MainWindow::on_ADD_released(){ ui->ADD-> setIcon(QIcon(":/Photo/ .png")); ui->ADD-> setIconSize(QSize(51,51));}/**********- - -***********/void MainWindow::on_SUB_pressed(){ ui->SUB-> setIcon(QIcon(":/Photo/-白.png")); ui->SUB-> setIconSize(QSize(51,51)); temp1 = line; //获取前面输入的字符,并临时保存起来 pointFlag = true; qDebug()<<temp1; line.clear(); //清空字符数组 if(flag3 == 0) { sum -= (temp1.toDouble()) * -1; //将字符转换成浮点型 } else { sum -= temp1.toDouble(); //将字符转换成浮点型 } qDebug()<<sum; flag3 = 1; ui->lcdNumber->display(0);//显示清空状态}
void MainWindow::on_SUB_released(){ ui->SUB-> setIcon(QIcon(":/Photo/-.png")); ui->SUB-> setIconSize(QSize(51,51));}/**********- C -***********/void MainWindow::on_CLEAR_pressed(){ ui->CLEAR-> setIcon(QIcon(":/Photo/C白.png")); ui->CLEAR-> setIconSize(QSize(51,51)); line.clear(); temp1.clear(); temp2.clear(); sum = 0; sum1 = 1; flag = 0; flag1 = 0; flag3 = 0; flag4 =0; flag5 = 0; pointFlag = true; rideFlag = true; ui->lcdNumber->display("0");}
void MainWindow::on_CLEAR_released(){ ui->CLEAR-> setIcon(QIcon(":/Photo/C.png")); ui->CLEAR-> setIconSize(QSize(51,51));}/**********- X -***********/void MainWindow::on_DIVIDE_pressed(){ ui->DIVIDE->setIcon(QIcon(":/Photo/X白.png")); ui->DIVIDE->setIconSize(QSize(51,51)); pointFlag = true; flag5 = 1; temp1 = line; //获取前面输入的字符,并临时保存起来 qDebug()<<temp1; line.clear(); //清空字符数组 if(sum == 0) { sum = temp1.toDouble(); } else { sum = sum * temp1.toDouble(); } ui->lcdNumber->display(0);//显示清空状态}
void MainWindow::on_DIVIDE_released(){ ui->DIVIDE->setIcon(QIcon(":/Photo/X.png")); ui->DIVIDE->setIconSize(QSize(51,51));}/**********- / -***********/void MainWindow::on_RIDE_pressed(){ ui->RIDE-> setIcon(QIcon(":/Photo/除白.png")); ui->RIDE-> setIconSize(QSize(51,51)); pointFlag = true; flag4 = 1; temp1 = line; //获取前面输入的字符,并临时保存起来 qDebug()<<"ride:"<<temp1; line.clear(); //清空字符数组 if(rideFlag == true) { rideFlag = false; sum1 = temp1.toDouble(); } else { sum1 = sum1 / temp1.toDouble(); } qDebug()<<"ridesum1:"<<sum1; ui->lcdNumber->display(0);//显示清空状态}
void MainWindow::on_RIDE_released(){ ui->RIDE-> setIcon(QIcon(":/Photo/除.png")); ui->RIDE-> setIconSize(QSize(51,51));}/**********- % -***********/void MainWindow::on_PER_CENT_pressed(){ ui->PER_CENT->setIcon(QIcon(":/Photo/%白.png")); ui->PER_CENT->setIconSize(QSize(51,51)); pointFlag = true;}
void MainWindow::on_PER_CENT_released(){ ui->PER_CENT->setIcon(QIcon(":/Photo/%.png")); ui->PER_CENT->setIconSize(QSize(51,51));}/**********- 。 -***********/void MainWindow::on_POINT_pressed(){ ui->POINT-> setIcon(QIcon(":/Photo/.白.png")); ui->POINT-> setIconSize(QSize(51,51));
if(pointFlag == true) //没有按等于号按键,说明没有进行运算,所以显示第一次输入的字符串 { pointFlag = false; line ="."; ui->lcdNumber->display(line); }}
void MainWindow::on_POINT_released(){ ui->POINT-> setIcon(QIcon(":/Photo/..png")); ui->POINT-> setIconSize(QSize(51,51));}/**********- = -***********/void MainWindow::on_EQUAL_pressed(){ ui->EQUAL-> setIcon(QIcon(":/Photo/=白.png")); ui->EQUAL-> setIconSize(QSize(51,101)); flag1 = 1; //按一次等于号就改变状态 if(flag == 1) //加法 { temp2 =line; line.clear(); //清空字符串 sum = sum temp2.toDouble(); //将字符转换成浮点型 ui->lcdNumber->display(QString::number(sum)); //将浮点型转换成字符 flag = 0; //重新让标志位置零,等待下一次的加法进行 } else if(flag3 == 1) //减法 { temp2 = line; line.clear(); //清空字符串 sum =sum - temp2.toDouble(); //将字符转换成浮点型 ui->lcdNumber->display(line);
if(sum == 0) //等于0 { ui->lcdNumber->display("0"); } ui->lcdNumber->display(QString::number(sum)); //将浮点型转换成字符 // qDebug()<<sum; flag3 = 0; //重新让标志位置零,等待下一次的减法进行 } else if(flag4 == 1) //除法 { temp2 = line; line.clear(); //清空字符串 rideFlag = true; qDebug()<<"temp2:"<<temp2; qDebug()<<"sum1:"<<sum1; sum1 = sum1 / temp2.toDouble(); //将字符转换成浮点型 if(temp2 == "0") //分母等于0 { ui->lcdNumber->display("0"); } qDebug()<<"sum11"<<sum1; ui->lcdNumber->display(QString::number(sum1)); //将浮点型转换成字符 flag4 = 0; //重新让标志位置零,等待下一次的除法进行 } else if(flag5 == 1) //乘法 { temp2 = line; line.clear(); //清空字符串 sum = sum * temp2.toDouble(); //将字符转换成浮点型 ui->lcdNumber->display(QString::number(sum)); //将浮点型转换成字符 flag5 = 0; //重新让标志位置零,等待下一次的乘法进行 }}
void MainWindow::on_EQUAL_released(){ ui->EQUAL-> setIcon(QIcon(":/Photo/=.png")); ui->EQUAL-> setIconSize(QSize(51,101));}
void MainWindow::on_actionscience_triggered(){ this->hide(); S.show();}
void MainWindow::on_actionprogrammer_triggered(){ this->hide(); P.show();}
评论